package com.uniview.exception;

import com.uniview.dto.RespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author lux
 * @date: 2019/11/26
 * @desc: 全局异常拦截器
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class CommonExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<RespDTO> handleException(Exception e) {
        RespDTO resp = new RespDTO();
        if(e instanceof CommonException){
            log.info("commonException:====>>>>>"+e.getMessage());
            CommonException commonException = (CommonException) e;
            resp.code = commonException.getErrorCode();
            resp.error = commonException.getErrorMsg();
            return new ResponseEntity(resp, HttpStatus.valueOf(resp.status));
        }else{
            log.info("Exception:=====>>>>>"+e.getMessage());
            resp.setCode("-1");
            resp.setError("操作失败");
            return new ResponseEntity(resp, HttpStatus.OK);
        }
    }
}
